package com.sise.bl.controller;

import com.sise.bl.utils.ImageUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.OutputStream;

/**
 * 生成验证码的controller
 */
@Controller
@RequestMapping(value = "/code")
public class CodeController {
    //生成验证码图片
    @RequestMapping(value = "/createCode")
    public void createCode(HttpServletRequest request, HttpServletResponse response) throws Exception{
        //第一个参数是生成的验证码，第二个参数是生成的图片
        Object[] objs = ImageUtils.createImage();
        //将验证码存入Session
        request.getSession().setAttribute("code",objs[0]);
        //将图片输出给浏览器
        BufferedImage image = (BufferedImage) objs[1];
        // 禁止图像缓存。
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/jpeg");
        OutputStream os = response.getOutputStream();
        ImageIO.write(image, "jpeg", os);
        os.flush();
    }
}
